#!/usr/bin/env python
# encoding: utf-8
'''
Created on Jun 6, 2011

@author: apple
'''

from flask import Module,request,render_template,flash,redirect,url_for,session

import catalogmodel as model

catalog = Module(__name__,name='catalog')

@catalog.route('/')
def index():
    cs = model.get_top_catalog()
    return render_template('catalog/index.html',cs = cs)
    
@catalog.route('/add',methods = ['POST','GET'])
def add():
    if request.method == 'GET':
        return render_template('catalog/add.html')
    elif request.method == 'POST':
        if not '_id' in session:
            return redirect(url_for('user.login'))
        #assert 0,str(dir(request.form))
        catalog = dict(filter(lambda _i:_i[1],zip(request.form.keys(),request.form.values())))
        catalog['creator'] = session['_id']
        assert catalog,str(catalog)
        result = model.add_catalog(catalog)
        if not result:
            flash('add catalog fail.')
            return render_template('catalog/add.html')
        else:
            return redirect(url_for('catalog.index'))